/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Properties Utility Object.
 * @author Adam
 * @version $Id: PropertiesUtils.java 16 2009-04-12 05:05:02Z a.ruggles $
 * 
 * Created on Oct 3, 2008 at 10:48:42 PM 
 */
public final class PropertiesUtils {
    /**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtils.class);

    /**
     * Loads a properties resource with a given name.
     * @param name The name of the desired resource.
     * @return A Properties object with the loaded resource.
     */
	public static Properties load(final String name) {
		final Properties properties = new Properties();
		final InputStream inStream = new Object() { }
			.getClass().getEnclosingClass().getResourceAsStream(name);
		LOG.info("Loading [{}]", name);
		try {
			properties.load(inStream);
		} catch (IOException e) {
			LOG.error("Exception occurred loading the resource", e);
		} finally {
			if (inStream != null) {
				try {
					inStream.close();
				} catch (IOException e) {
					LOG.error("IOException closing the input stream", e);
				}
			}
		}
		return properties;
	}

    /**
     * Loads a properties resource with a given name and with given defaults.
     * @param name The name of the desired resource.
     * @param defaultProps Default properties to set the Properties resource to.
     * @return A Properties object with the loaded resource.
     * @throws RuntimeException if an error occurs retrieving the resource.
     */
	public static Properties load(final String name, final Properties defaultProps) throws RuntimeException {
		final Properties properties = new Properties(defaultProps);
		final InputStream inStream = new Object() { }
			.getClass().getEnclosingClass().getResourceAsStream(name);
			LOG.info("Loading [{}]", name);
		try {
			properties.load(inStream);
		} catch (IOException e) {
			LOG.error("Exception occurred loading the resource", e);
		} finally {
			if (inStream != null) {
				try {
					inStream.close();
				} catch (IOException e) {
					LOG.error("IOException closing the input stream", e);
				}
			}
		}
		return properties;
	}

    /**
     * Private Constructor.
     */
    private PropertiesUtils() { }
}
